package com.xuetang9.fujian.view;

import com.xuetang9.fujian.model.Group;
import com.xuetang9.fujian.model.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author: fujian
 * @description: 注册用户主面板
 * @date: 2021/6/7 11:31
 * @version: V1.0
 * @package: com.xuetang9.fujian.view
 **/
@SuppressWarnings("all")
public class RegisterFrame extends JFrame {
    private JPanel contentPane = null;
    private LoginFrame loginFrame = null;
    private JLabel lblNickName = new JLabel("昵称");
    private JTextField txtNickName = new JTextField();
    private JLabel lblPassword = new JLabel("密码");
    private JPasswordField txtPassword = new JPasswordField();
    private JLabel lblRePassword = new JLabel("确认密码");
    private JPasswordField txtRePassword = new JPasswordField();
    // 性别单选按钮
    private JLabel lblSex = new JLabel("性别");
    private JRadioButton maleRadioButton = new JRadioButton("男");
    private JRadioButton femalRadioButton = new JRadioButton("女");
    private JButton btnRegister = new JButton("注册");
    private JButton btnCancel = new JButton("取消");
    public RegisterFrame(){
        init();
        setVisible(true);
    }

    public RegisterFrame(LoginFrame loginFrame){
        this.loginFrame = loginFrame;
        init();
        setVisible(true);
    }

    private void init() {
        setTitle("注册");
        setSize(430,330);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
        contentPane = (JPanel) getContentPane();
        contentPane.setLayout(null);
        // 配置所有的组件
        configNickNameLabel();
        configNickNameTextField();
        configPasswordLabel();
        cofigPasswordTextField();
        configRePasswordLabel();
        configRePasswordTextField();
        configSexLabel();
        configMaleRadioButton();
        configFemaleRadioButton();
        configRegisterButton();
        configCancelButton();
        // 安装所有的组件
        installComponents();
        // 安装所有组件的事件
        installListeners();

    }

    private void configNickNameLabel() {
        lblNickName.setBounds(10,25,80,35);
        lblNickName.setHorizontalAlignment(JLabel.RIGHT);
    }

    private void configNickNameTextField() {
        txtNickName.setBounds(100,25,238,35);

    }

    private void configPasswordLabel() {
        lblPassword.setBounds(10,75,80,35);
        lblPassword.setHorizontalAlignment(JLabel.RIGHT);


    }

    private void cofigPasswordTextField() {
        txtPassword.setBounds(100,75,238,35);
    }

    private void configRePasswordLabel() {
        lblRePassword.setBounds(10,125,80,35);
        lblRePassword.setHorizontalAlignment(JLabel.RIGHT);

    }

    private void configRePasswordTextField() {
        txtRePassword.setBounds(100,125,238,35);
    }

    private void configSexLabel() {
        lblSex.setBounds(10,175,80,35);
        lblSex.setHorizontalAlignment(JLabel.RIGHT);


    }

    private void configMaleRadioButton() {
        maleRadioButton.setBounds(100,175,60,35);
    }

    private void configFemaleRadioButton() {
        femalRadioButton.setBounds(220,175,60,35);
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(maleRadioButton);
        buttonGroup.add(femalRadioButton);
        maleRadioButton.setSelected(true);
    }

    private void configRegisterButton() {
        btnRegister.setBounds(260,225,70,30);
        btnRegister.setContentAreaFilled(false);
        btnRegister.setCursor(new Cursor(Cursor.HAND_CURSOR));
    }

    private void configCancelButton() {
        btnCancel.setBounds(335,225,70,30);
        btnCancel.setContentAreaFilled(false);
        btnCancel.setCursor(new Cursor(Cursor.HAND_CURSOR));
    }

    private void installComponents() {
        contentPane.add(lblNickName);
        contentPane.add(txtNickName);
        contentPane.add(lblPassword);
        contentPane.add(txtPassword);
        contentPane.add(lblRePassword);
        contentPane.add(txtRePassword);
        contentPane.add(lblSex);
        contentPane.add(maleRadioButton);
        contentPane.add(femalRadioButton);
        contentPane.add(btnRegister);
        contentPane.add(btnCancel);
    }

    private void installListeners() {
        btnRegister.addActionListener(new RegisterButtonHandler());
        btnRegister.addMouseListener(new RegisterButtonHandler());
        btnCancel.addActionListener(new CancelButtonHandler());
    }

    private class RegisterButtonHandler extends MouseAdapter implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            // 监测注册的内容，如果合法，就将注册的信息发送给服务器，服务器会返回一个登录的账号
            String nickName = txtNickName.getText();
            String  password = new String(txtPassword.getPassword());
            String sex = maleRadioButton.isSelected() ? "男" : "女";
            User user = new User();
            user.setSex(sex);
            user.setSignature("signature");
            user.setNickName(nickName);
            user.setPassword(password);
            user.setHeadPath(null);
            HashMap<Group, List<String>> group = new LinkedHashMap<>();
            group.put(new Group("我的好友"),new ArrayList<>());
            group.put(new Group("黑名单"),new ArrayList<>());
            user.setGroups(group);
            HashMap<String, Object> message = new HashMap<>();
            message.put("handlerClass","com.xuetang9.fujian.net.request.RegisterRequestHandler");
            message.put("registerUser",user);
            MainFrame.client.sendMessage(message);
        }
    }

    private class CancelButtonHandler extends MouseAdapter implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            RegisterFrame.this.dispose();
            loginFrame.setVisible(true);
        }
    }

    public static void main(String[] args) {
        new RegisterFrame();
    }
}
